package com.fangcang.lambda;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

/**
 * java 8新特性详解
 * 
 * @author fangcang
 * @date 2019年2月14日
 * @description
 */
public class Java8NewFeatureUtil {

	public static void main(String[] args) {
		lambdaTest();
		
		lambdaTest2();
		
		iterateCollection();
		
		testPredicate();
	}
	

	public static void lambdaTest() {
		// jdk8之前启动线程方式
		new Thread(new Runnable() {
			@Override
			public void run() {
				System.out.println("start thread with less than of jdk7");
			}
		}).start();
		//(params) -> statement
		new Thread(() -> System.out.println("start thread with jdk 8")).start();
		//(params) -> { statements }
		new Thread(() -> {
			System.out.println("start thread with jdk 8");
		}).start();
	}
	
	
	
	public static void lambdaTest2() {
		//jdk 8之前
		List<Object> list = new LinkedList<Object>();
		list.add("12");
		list.add("123");
		list.add("1234");
		Collections.sort(list,  new Comparator<Object>() {
			@Override
			public int compare(Object o1, Object o2) {
				return o1.hashCode()-o2.hashCode();
			}
		});
		
		System.out.println(list.toString());
		
		//jdk8  使用lambda取代匿名内部类
		Collections.sort(list, (o1, o2) ->{
			return o2.hashCode()-o1.hashCode();
		});
		
		System.out.println(list.toString());
	}
	
	
	/**
	 * 使用jdk8 Lambda遍历集合
	 */
	public static void iterateCollection() {
		List<String> list = Arrays.asList("12", "123", "1234");
		for(String str : list) {
			System.out.println(str);
		}
		
		list.forEach(str -> System.out.println(str));
		
		list.forEach(System.out::println);
		
		Map<String, String> map = new HashMap<String, String>();
		map.put("a", "abc");
		map.put("b", "abcd");
		map.put("c", "abcde");
		map.forEach((key, value) -> {
			System.out.println("key=" + key + ", value=" + value);
		});
	}
	
	/**
	 * 使用函数接口Predicate过滤数据
	 * @param names
	 * @param condition
	 */
	public static void filter(List<String> names, Predicate<String> condition) {
		if(null == names || names.isEmpty()){
			return ;
		}
		
		names.forEach(name ->{
			if(condition.test(name)) {
				System.out.println(name);
			}
		});
	}
	
	
	public static void filter2(List<String> names, Predicate<String> condition) {
		if(null == names || names.isEmpty()){
			return ;
		}
		
		names.stream().filter((name)->condition.test(name)).forEach((name)->{
			System.out.println(name + " ");
		});
	}
	
	/**
	 * 使用Predicate 的and、or、xor进行多条件过滤
	 * @param names
	 * @param condition
	 */
	@SuppressWarnings("unchecked")
	public static void filter3(List<String> names, Predicate<String>... conditions) {
		if(null == names || names.isEmpty()){
			return ;
		}
		
		if(null == conditions || conditions.length == 0){
			return ;
		}
		
		Predicate<String> main = null;
		for(Predicate<String> condition: conditions) {
			if(main == null) {
				main = condition;
			}
			main.and(condition);
		}
		final Predicate<String> mainPredicate = main;
		
		names.stream().filter((name)->mainPredicate.test(name)).forEach((name)->{
			System.out.println(name + " ");
		});
	}
	
	@SuppressWarnings("unchecked")
	public static void  testPredicate() {
		List<String> names = Arrays.asList("zhangsan", "lisi", "wangwu", "zhaoliu");
		//过滤首字母不为z的姓名
		System.out.println("过滤以z开头的姓名：");
		filter(names, (name)->name.startsWith("z"));
		//过滤长度小于5的姓名
		System.out.println("过滤长度大于5位的姓名：");
		filter(names, (name)->name.length() > 5);
		System.out.println("过滤包含l的姓名：");
		filter2(names,  (name)->name.contains("l"));
		System.out.println("过滤以z开头并且长度大于5位的姓名：");
		filter3(names, (name)->name.startsWith("z"), (name)->name.length() > 5);
	}
	
}
